$(function() {
	getstru();
	getRole();
	getPosition();
	//getSecStru();
	$("#creatUser").click(function() {
		creatUser();
	})
	$(".department").change(function() {
		$(".child").html("");
		str = '<option value="choice">请选择分监区</option>';
		$(".child").append(str);
		var structureId = $(this).val();
		if (structureId != "choice") {
			getPosition();
			if ($(".department :selected").attr("data-num") == '0') {
				YuXi.util.get("getAllStructureBySuperId.do", {
					superId: structureId
				}, function(data) {
					var str = "";
					var object = data.object;
					$(".child").html("");
					str = '<option value="choice">请选择分监区</option>';
					$(".child").append(str);
					for (var i = 0; i < object.length; i++) {
						str = '<option value="' + object[i].id + '">' + object[i].name + '</option>';
						$(".child").append(str);
					}

				}, function() {

				});
			}
		}
	});
	$(".child").change(function() {
		var structureId = $(this).val();
		if (structureId != "choice") {
			getPosition();
		}
	});

	function getRole() {

		var url = "getAllRole.do"; //根据部门获取角色地址
		YuXi.util.get(url, "null", function(data) {
			var str = "";
			var object = data.object;
			$(".roleList").html("");
			//		if(object.length==0){
			//			str='<option value="''">'+object[i].name+'</option>';
			//			$(".position").append(str);	
			//		}
			for (var i = 0; i < object.length; i++) {
				str = '<option value="' + object[i].id + '">' + object[i].roleName + '</option>';
				$(".roleList").append(str);
			}
			//console.log("获取角色成功");
		}, function() {
			//console.log("获取角色失败");
		})
	}

	function getPosition() {

		var url = "getAllPsoition.do"; //根据部门获取角色地址
		YuXi.util.get(url, "null", function(data) {
			var str = "";
			var object = data.object;
			$(".position").html("");
			
			for (var i = 0; i < object.length; i++) {
				str = '<option value="' + object[i].id + '">' + object[i].positionName + '</option>';
				$(".position").append(str);
			}
			
		}, function() {
			
		})
	}

	/* 根据当前角色所属部门Id获取他下级的所有部门 */
	function getstru() {
		var url = "getDepartmentAndArea.do"; // 获取部门
		var data = {};
		var object = {};
		data = YuXi.util.get(url, null, function(data) {
			object = data.object;
			for (var i = 0; i < object.length; i++) {
				str = '<option data-num=' + object[i].num + ' value="' + object[i].id + '">' + object[i].name + '</option>';
				$(".department").append(str);
			}

			
			


		}, function() {

		})
	}

	/* 创建用户 */
	function creatUser() {
		var user = {};
		var structureId;
		var structureName;
		
		  if($(".child").val()!="choice"){
			  structureId=$(".child :selected").val();
			  structureName=$(".child :selected").text();
		  }else if($(".department").val()!="choice"){
			 
			 structureId=$(".department :selected").val();
			 structureName=$(".department :selected").text();
		  }else{
			 
			  structureId=$(".struc :selected").val();
			  structureName=$(".struc :selected").text();
		  }
		user.userName = $("input[name=username]").val(); // 用户名
		user.userPassword = $("input[name=password]").val(); // 密码
		user.userAccount = $("input[name=accout]").val(); //
		user.idNumber = $("input[name=identity]").val(); // 身份证
		user.phone = $("input[name=phone]").val(); // 电话
		user.positionId = $('.position option:selected').val(); // 职务id
		user.positionName = $('.position option:selected').text(); // 职务名称
		user.roleId = $('.roleList option:selected').val(); // 角色id
		user.structureId=structureId;
		user.structureName=structureName;
		
		if (user.userName == null || user.userName == "") {
			myTool.alert({
				title: "提示",
				context: "请输入用户名"
			});
			return false;
		}

		if (user.userAccount.length<6 || user.userAccount.length>20) {
			myTool.alert({
				title: "提示",
				context: "警号长度为6~20"
			});
			return false;
		}

		if (user.userAccount == null || user.userAccount == "") {
			myTool.alert({
				title: "提示",
				context: "请输入警号"
			});
			return false;
		}
		var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
		if (user.idNumber == null || user.idNumber == "" || !reg.test(user.idNumber)) {
			myTool.alert({
				title: "提示",
				context: "请输入正确的身份证号"
			});
			return false;
		}
		var phone = /^1[3|4|5|7|8]\d{9}$/;
		if (user.phone == null || user.phone == "" || !phone.test(user.phone)) {
			myTool.alert({
				title: "提示",
				context: "请输入正确的电话号码"
			});
			return false;
		}
		if (user.userPassword == null || user.userPassword == "") {
			myTool.alert({
				title: "提示",
				context: "请输入密码"
			});
			return false;
		}
		if (user.roleId == null || user.roleId == "") {
			myTool.alert({
				title: "提示",
				context: "请选择职务"
			});
			return false;
		}
		addUser(user);
	}

	// 添加用户
	function addUser(user) {
		var url = "addUser.do"; // 新建用户地址
		YuXi.util.post(url, user, function(data) {
			if (data.status) {
				myTool.alert({
					title: "提示",
					context: data.msg,
					callback: function() {
						window.location.href = "userList.jsp";
					}
				});

			} else {
				myTool.alert({
					title: "提示",
					context: data.msg
				});
			}
		}, function() {
			//console.log("新建用户失败");
		})
		return false;
	}
	/* 根据角色id获取下级角色 */
	function getSplit() {
		// var
		// object=[{"id":4,"name":"监区长","super_id":7,"structure_id":4,"remark":"faeiov"},
		// {"id":5,"name":"监狱长","super_id":7,"structure_id":4,"remark":"faeiov"},
		// {"id":6,"name":"机关民警","super_id":7,"structure_id":4,"remark":"faeiov"}];
		// var str="";
		// var sStart='<div class="col-sm-2"><select class="form-control"
		// ><option></option>';
		// var sEnd='</select></div> ';
		// for(var i=0;i<object.length;i++){
		// str+='<option onClick="getSplit('+object[i].id+')"
		// value="'+object[i].id+'">'+object[i].name+'</option>';
		// }
		// str=sStart+str+sEnd;
		// $(".pRole").append(str);
	}

	/* 根据super_id向后台请求二级部门信息 */
	function getSecStru(superId) {
		var url = "getAllStructureBySuperId.do"; // 获取部门
		var data = {};
		var object = {};
		var shtml = '<div class="col-sm-2"><select class="form-control struc">';
		var ehtml = '</select></div> ';
		var html = "";
		data = YuXi.util.get(url, function(data) {
			object = data.object;
			for (var i = 0; i < object.length; i++) {
				html += '<option value="' + object[i].id + '" onClick="getSecStru(' + object[i].id + ')">' + object[i].name + '</option>';
			}
			html = shtml + html + ehtml;
			$(".stru").append(html);
		}, function() {});

		//console.log("部门====" + data);
		/* 二级部门 */

	};
});